Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 3D coordinates supports to GeoJSON reader and writer #1150

Merged
merged 1 commit into from
Aug 22, 2024

Conversation

Oreilles
Copy link
Contributor

@Oreilles Oreilles commented Aug 21, 2024

This PR adds 3D (Z) coordinates support to GeoJSON reader and writer.

It includes a new method setOutputDimensions for the writer, similar to the WKT one, but which (for now ?) only accepts values 2 or 3. Support for M coordinates was left out of this PR as it is discouraged by the specification, and would require more thinking about special cases. Any M values in the coordinates being serialized are therefore ignored (like the Z values were before this PR).

Considering the handling of mixed 2D/3D coordinates and NaN values isn't clearly specified, I made some decisions in that regard which are of course all open for debate:

  • Mixed coordinates are allowed. If a coordinate sequence contains any Z value, the missing coordinates in the parsed geometry will be NaN. This behavior is different than PostGIS and OGR, which both interpret missing Z coordinates as 0:
SELECT '{"type":"LineString","coordinates":[[-117, 33], [-116, 34, 4]]}'::geometry;
-- LINESTRING(-117 33 0,-116 34 4)
>>> ogr.CreateGeometryFromJson("{\"type\":\"LineString\",\"coordinates\":[[-117, 33], [-116, 34, 4]]}")
LINESTRING Z (-117 33 0,-116 34 4)

As a side note, PostGIS also converts any null value in a GeoJSON string as 0:

SELECT '{"type":"Point","coordinates":[1,null,null]}'::geometry;
-- POINT(1 0 0)

But OGR will instead consider it an invalid input:

>>> ogr.CreateGeometryFromJson("{\"type\":\"Point\",\"coordinates\":[0, null, null]}")
None
  • NaN Z values are never included in the coordinate array, regardless of the writer output dimension. This choice was made to facilitate interoperability and round-trips within GEOS, as parsing a GeoJSON string with nulls will result in a ParsingError... But maybe this is something we want to change, even though it would deviate from the standard which states that the coordinates GeoJSON member should only contain numbers.

@Oreilles Oreilles force-pushed the geojson-3d branch 2 times, most recently from 76335ec to a73d9d9 Compare August 21, 2024 12:36
@pramsey
Copy link
Member

pramsey commented Aug 21, 2024

Can you rebase main, that should clear up CI.

@Oreilles
Copy link
Contributor Author

Done.

@pramsey pramsey requested a review from dbaston August 21, 2024 21:32
@pramsey pramsey merged commit 0296a92 into libgeos:main Aug 22, 2024
28 checks passed
@pramsey pramsey removed the request for review from dbaston August 22, 2024 15:59
pramsey added a commit that referenced this pull request Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants